package com.yw.atp.action;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.yw.atp.model.Consult;
import com.yw.atp.service.ConsultService;
import com.yw.atp.service.PersistenceService;
import com.yw.atp.util.StringUtil;
import com.yw.atp.web.ActionBase;

@Scope("prototype")
@Component("consultAction")
public class ConsultAction extends ActionBase implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	@Autowired
	private PersistenceService service;
	@Autowired
	private ConsultService consultService;
	HttpServletRequest request;
	public List<Consult> consultList;
	public Consult consult;
	public String consultIds;
	public String res;
	public int start = 0;
	public int limit = 20;
	public String list(){
		if(consult!=null&&consult.getContent()!=null){
			consultList = consultService.getConsults(consult);
		}else{
			consultList = service.findAll(Consult.class,0);
		}
		JSONObject js = new JSONObject();
		try {
			if(consultList!=null&&consultList.size()>0){
				js.put("totalCount",consultList.size());
				int len = consultList.size();
				start = start>(len-1)?(len-1):start;
				limit = limit>(len-start)?(len):(start+limit);
				js.put("topics",Consult.capture(consultList.subList(start,limit)));
			}
			res = js.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String add(){
		if(StringUtil.checkConsult(consult)){
			if(consult.getId()==null||consult.getId().intValue()<1){
				if(consult.getCreatedAt()==null){consult.setCreatedAt(new Date());};
			}
			service.save(consult);
			return SUCCESS;
		}else{
			return INPUT;
		}
	}
	
	public String del(){
		if(consultIds!=null&&!"".equals(consultIds)){
			service.remove(Consult.class.getName(),consultIds);
		}
		return SUCCESS;
	}
	
	public void setServletRequest(HttpServletRequest request) {
        if(request==null){
            request = ServletActionContext.getRequest();
        }
        this.request = request;
    }
	
	public PersistenceService getService() {
		return service;
	}
	
	public void setService(PersistenceService service) {
		this.service = service;
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public String getRes() {
		return res;
	}

	public void setRes(String res) {
		this.res = res;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	

	public ConsultService getConsultService() {
		return consultService;
	}

	public void setConsultService(ConsultService consultService) {
		this.consultService = consultService;
	}

	public List<Consult> getConsultList() {
		return consultList;
	}

	public void setConsultList(List<Consult> consultList) {
		this.consultList = consultList;
	}

	public Consult getConsult() {
		return consult;
	}

	public void setConsult(Consult consult) {
		this.consult = consult;
	}

	public String getConsultIds() {
		return consultIds;
	}

	public void setConsultIds(String consultIds) {
		this.consultIds = consultIds;
	}
}
